﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Web;

namespace TaobaoPick
{
    public class HTMLHelpers
    {

        public class UrlComposite
        {
            private String oriUrl="";
            private String url="";
            private String path="";
            private Hashtable parameters=new Hashtable();

            public String OriUrl
            {
                get { return oriUrl; }
                set { oriUrl = value; }
            }
            public String Url
            {
                get { return url; }
                set { url = value; }
            }
    
            public String Path
            {
                get { return path; }
                set { path = value; }
            }
            public Hashtable Params
            {
                get { return parameters; }
                set { parameters = value; }
            }
            public String createURL()
            {
                StringBuilder sb=new StringBuilder();
                foreach(String k in parameters.Keys)
                {
                    sb.Append(k);
                    sb.Append("=");
                    sb.Append(HttpUtility.UrlEncode((String)parameters[k]));
                    sb.Append("&");
                }
                String r=sb.ToString() ;
                r = url+"?"+r;
                return r;
            }

        }
        public static UrlComposite parseURL(String url)
        {
            int paramidx = url.LastIndexOf('?');
            UrlComposite result = new UrlComposite();
            result.Params = parseParameter(url);
            result.OriUrl = url;

            if (paramidx > 0)
            {
                url = url.Substring(0, paramidx);
                int pidx=url.LastIndexOf('/');
                result.Path = url.Substring(0, pidx);
                result.Url = url;
            }            
            return result;
        }
        public static Hashtable parseParameter(String url)
        {
            url = url.Replace("&amp;", "&");
            int pos = url.IndexOf("?");
            Hashtable result = new Hashtable();
            if (pos > 0)
            {
                url = url.Substring(pos + 1);
                String r =  HttpUtility.UrlDecode(url, System.Text.Encoding.GetEncoding("gbk"));
                
                String[] ss = r.Split('&');
                
                foreach (String s in ss)
                {
                    pos = s.IndexOf("=");
                    if (pos < 0)
                        continue;
                    String s1 = s.Substring(0, pos);
                    String s2 = s.Substring(pos + 1);
                    result[s1] = s2;
                }
            }
            return result;
        }
        public static void printHash(Hashtable t)
        {
            foreach (DictionaryEntry entry in t)
            {
                Console.WriteLine("{0}, {1}", entry.Key, entry.Value);
            }
        }
    }
}
